package user

import (
	"crypto/md5"
	"encoding/hex"
)

type Password struct {
	value   string
	encrypt string
	valid   bool
}

func NewPassword(value string) *Password {
	p := &Password{}
	p.Set(value)
	p.Valid()
	return p
}

func (p *Password) Set(value string) {
	p.value = value
}

func (p *Password) SetEncrypt(value string) {
	p.encrypt = value
}

func (p *Password) Get() string {
	return p.value
}

func (p *Password) Check() bool {
	if p.Encrypt() != p.encrypt {
		return false
	}
	return true
}

func (p *Password) Encrypt() string {
	// 加密规则，需要同步和java实现
	hash := md5.New()
	hash.Write([]byte(p.value))
	return hex.EncodeToString(hash.Sum(nil))[:20]
}

func (p *Password) Valid() {
	if len(p.value) < 6 {
		p.valid = false
	}
	p.valid = true
}

func (p *Password) GetValid() bool {
	return p.valid
}
